package com.meiborte.dao.impl;

import com.meiborte.dao.BookDao;
import com.meiborte.entity.Book;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookDaoimpl implements BookDao {

    public DataSource dataSource;

    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public List<Book> findAll() {
        String sql = "select * from books";
        List<Book> books = new ArrayList<>();
        Connection connection=null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection=dataSource.getConnection();
            statement =connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                Book book = new Book();
                book.setId(resultSet.getInt("id"));
                book.setTitle(resultSet.getString("title"));
                book.setPrice(resultSet.getDouble("price"));
                book.setQuantity(resultSet.getInt("quantity"));
                books.add(book);


            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return books;
    }
    @Override
    public int addBook(Book book) {
        String sql = "insert into books(title,price,quantity) values (?,?,?)";

        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = dataSource.getConnection();
            connection.setAutoCommit(true);
           statement = connection.prepareStatement(sql);
           statement.setString(1, book.getTitle());
           statement.setDouble(2, book.getPrice());
           statement.setInt(3, book.getQuantity());
           return statement.executeUpdate();
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public int updateBook(Book book) {
        String sql = "update books set title=?,price=?,quantity=? where id=?";
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = dataSource.getConnection();
            statement = connection.prepareStatement(sql);
            statement.setString(1,book.getTitle());
            statement.setDouble(2,book.getPrice());
            statement.setInt(3,book.getQuantity());
            return statement.executeUpdate();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
    @Override
    public int deleteBook(int id) {
        String sql = "delete from books where id=?";
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            connection=dataSource.getConnection();
            statement = connection.prepareStatement(sql);
            return statement.executeUpdate();
        }catch (SQLException e){
            throw new RuntimeException(e);
        }
    }
}
